package com.service.impl;

import com.service.IndexService;
import com.utils.ConnectionUtils;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.json.JsonXContent;


public class IndexServiceImpl  implements IndexService {
    @Override
    public void createIndex(String index) throws Exception {

        if (exist(index)) {
            //true代表存在,存在就不创建
            System.err.println("index" + index + "已经存在了");
            return;
        }

        //先获取连接
        RestHighLevelClient client = ConnectionUtils.getClient();
        //创建库的请求对象,设置我们的index名字
        CreateIndexRequest request = new CreateIndexRequest(index);

        //设置分片和列
        //设置分片
        Settings settings = Settings.builder()
                .put("number_of_shards", 3)
                .put("number_of_replicas", 1)
                .build();
        //设置列
        XContentBuilder xContentBuilder = JsonXContent.contentBuilder()
                .startObject()
                .startObject("properties")
                .startObject("corpName").field("type", "keyword").endObject()
                .startObject("longCode").field("type", "keyword").endObject()
                .startObject("mobile").field("type", "keyword").endObject()
                .startObject("province").field("type", "keyword").endObject()
                .startObject("fee").field("type", "integer").endObject()
                .startObject("operatorId").field("type", "integer").endObject()
                .startObject("replyTotal").field("type", "integer").endObject()
                .startObject("state").field("type", "integer").endObject()
                .startObject("ipAddr").field("type", "ip").endObject()
                .startObject("createDate").field("type", "date").field("format", "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis").endObject()
                .startObject("sendDate").field("type", "date").field("format", "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis").endObject()
                .startObject("smsContent").field("type", "text").field("analyzer", "ik_max_word").endObject()
                .endObject()
                .endObject();
        //给请求设置分片和列的数据
        request.settings(settings)
                .mapping(xContentBuilder);

        //创建库
        CreateIndexResponse response = client.indices()//这里面的都是和index相关的操作
                .create(request, RequestOptions.DEFAULT);
        System.err.println(response.isAcknowledged());
    }

    @Override
    public boolean exist(String index) throws Exception {
        //获取连接
        RestHighLevelClient client = ConnectionUtils.getClient();
        return client.indices()//关于index的操作
                .exists(new GetIndexRequest(index), RequestOptions.DEFAULT);//判断index是否存在
    }

    @Override
    public void getIndex(String index) throws Exception {
        RestHighLevelClient client = ConnectionUtils.getClient();
        GetIndexResponse response = client.indices().get(new GetIndexRequest(index), RequestOptions.DEFAULT);
        System.err.println(response.getMappings().get("gp13-zm").getSourceAsMap());//查询index的映射关系
    }

    @Override
    public void deleteIndex(String index) throws Exception {
        RestHighLevelClient client = ConnectionUtils.getClient();
        AcknowledgedResponse response = client.indices().delete(new DeleteIndexRequest(index),//请求对象
                                                                RequestOptions.DEFAULT);
        System.err.println(response.isAcknowledged());
    }
}
